﻿using Microsoft.Practices.Unity;
using Newtonsoft.Json;
using NPOI.HSSF.UserModel;
using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading;
using System.Web.Mvc;
using Wicresoft.UnifyShow.Business;
using Wicresoft.UnifyShow.Utility;
using Wicresoft.UnifyShow2.Portal.Extensions;

namespace Wicresoft.UnifyShow2.Portal.Controllers
{
    [Authorize]
    public class ReportViewController : BaseController
    {
        private IUser_X user_x;
        private ISiteBranch siteBranch;
        private IReportView reportView;

        protected override void Dispose(bool disposing)
        {
            ((IDisposable)user_x).Dispose();
            ((IDisposable)reportView).Dispose();
            ((IDisposable)siteBranch).Dispose();
        }

        [InjectionConstructor]
        public ReportViewController(ISiteBranch _siteBranch, IUser_X _user_x, IReportView _report)
        {
            siteBranch = _siteBranch;
            user_x = _user_x;
            reportView = _report;
        }

        public CultureInfo Culture
        {
            get
            {
                return new System.Globalization.CultureInfo("zh-CN");
            }
        }

        public ActionResult Index()
        {
            Thread.CurrentThread.CurrentCulture = Culture;
            Thread.CurrentThread.CurrentUICulture = Culture;

            SetRoles(user_x);

            ViewBag.IsConfigAdmin = IsConfigAdmin;
            ViewBag.IsPlanManage = true;

            var CurrentUser = user_x.GetUser_XByName(User.Identity.Name);
            if (CurrentUser == null)
            {
                throw new Exception("USER_X is null.");
            }
            var sites = user_x.GetSiteBranchByID(CurrentUser.ID);
            var t = sites.FirstOrDefault(s => s.Name == CurrentUser.Company);
            if (t == null)
            {
                throw new Exception("UserSite is null.");
            }

            ViewBag.UserDept = CurrentUser.Department;
            ViewBag.UserCompany = CurrentUser.Company;

            TempData["Companys"] = siteBranch.GetSiteALLBranch().Where(x => !string.IsNullOrEmpty(x.Alias)).ToList();
            //省局用户
            TempData["IsGlobalUser"] = t.Alias.Equals("省局");
            //当前用户所在公司
            TempData["CurrentCompany"] = t;

            return View();
        }

        public JsonResult MenuList()
        {
            var root = new JsonCollection();

            JsonObject menu1 = new JsonObject();
            menu1.MergeProperty("text", new JsonConstant("合并工单查询"));
            menu1.MergeProperty("iconCls", new JsonConstant("icon-redo"));
            JsonObject attri = new JsonObject();
            attri.MergeProperty("targetHref", new JsonConstant("/ReportView/WorkOrderList"));
            menu1.MergeProperty("attributes", new JsonConstant(attri));
            menu1.MergeProperty("checked", new JsonConstant(true));
            root.AppendObject(menu1);

            JsonObject menu2 = new JsonObject();
            menu2.MergeProperty("text", new JsonConstant("事件综合查询"));
            menu2.MergeProperty("iconCls", new JsonConstant("icon-redo"));
            JsonObject attri2 = new JsonObject();
            attri2.MergeProperty("targetHref", new JsonConstant("/ReportView/EventList"));
            menu2.MergeProperty("attributes", new JsonConstant(attri2));
            root.AppendObject(menu2);

            JsonObject menu3 = new JsonObject();
            menu3.MergeProperty("text", new JsonConstant("变更综合查询"));
            menu3.MergeProperty("iconCls", new JsonConstant("icon-redo"));
            JsonObject attri3 = new JsonObject();
            attri3.MergeProperty("targetHref", new JsonConstant("/ReportView/ChangeList"));
            menu3.MergeProperty("attributes", new JsonConstant(attri3));
            root.AppendObject(menu3);

            JsonObject menu5 = new JsonObject();
            menu5.MergeProperty("text", new JsonConstant("任务查询"));
            menu5.MergeProperty("iconCls", new JsonConstant("icon-redo"));
            JsonObject attri5 = new JsonObject();
            attri5.MergeProperty("targetHref", new JsonConstant("/ReportView/TaskList"));
            menu5.MergeProperty("attributes", new JsonConstant(attri5));
            root.AppendObject(menu5);

            JsonObject menu6 = new JsonObject();
            menu6.MergeProperty("text", new JsonConstant("告警查询"));
            menu6.MergeProperty("iconCls", new JsonConstant("icon-redo"));
            JsonObject attri6 = new JsonObject();
            attri6.MergeProperty("targetHref", new JsonConstant("/ReportView/AlertList"));
            menu6.MergeProperty("attributes", new JsonConstant(attri6));
            root.AppendObject(menu6);

            JsonObject menu7 = new JsonObject();
            menu7.MergeProperty("text", new JsonConstant("硬件资产查询"));
            menu7.MergeProperty("iconCls", new JsonConstant("icon-redo"));
            JsonObject attri7 = new JsonObject();
            attri7.MergeProperty("targetHref", new JsonConstant("/ReportView/HardwareList"));
            menu7.MergeProperty("attributes", new JsonConstant(attri7));
            root.AppendObject(menu7);

            JsonObject menu8 = new JsonObject();
            menu8.MergeProperty("text", new JsonConstant("软件资产查询"));
            menu8.MergeProperty("iconCls", new JsonConstant("icon-redo"));
            JsonObject attri8 = new JsonObject();
            attri8.MergeProperty("targetHref", new JsonConstant("/ReportView/SoftwareList"));
            menu8.MergeProperty("attributes", new JsonConstant(attri8));
            root.AppendObject(menu8);

            JsonObject menu9 = new JsonObject();
            menu9.MergeProperty("text", new JsonConstant("应用系统资产查询"));
            menu9.MergeProperty("iconCls", new JsonConstant("icon-redo"));
            JsonObject attri9 = new JsonObject();
            attri9.MergeProperty("targetHref", new JsonConstant("/ReportView/BusinessSystemList"));
            menu9.MergeProperty("attributes", new JsonConstant(attri9));
            root.AppendObject(menu9);

            return new NRemedy.MVC.UI.RawJsonResult()
            {
                Data = root.ToString(),
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            };
        }

        public ContentResult WorkOrderList()
        {
            //客户公司
            var companys = TempData["Companys"];
            if (companys == null)
            {
                companys = siteBranch.GetSiteALLBranch().Where(x => !string.IsNullOrEmpty(x.Alias)).ToList();
            }
            ViewData["Companys"] = companys;
            TempData["Companys"] = companys;

            var isGlobal = TempData["IsGlobalUser"];
            var currentCompany = TempData["CurrentCompany"] as SiteBranchModel;

            if (isGlobal == null || currentCompany == null)
            {
                var CurrentUser = user_x.GetUser_XByName(User.Identity.Name);
                if (CurrentUser == null)
                {
                    throw new Exception("USER_X is null.");
                }
                var sites = user_x.GetSiteBranchByID(CurrentUser.ID);
                var t = sites.FirstOrDefault(s => s.Name == CurrentUser.Company);
                if (t == null)
                {
                    throw new Exception("UserSite is null.");
                }
                isGlobal = t.Alias.Equals("省局");
                currentCompany = t;
            }

            //省局用户
            ViewData["IsGlobalUser"] = isGlobal;
            TempData["IsGlobalUser"] = isGlobal;

            //用户所在公司
            ViewData["CurrentCompany"] = currentCompany;
            TempData["CurrentCompany"] = currentCompany;

            var html = this.GetHtmlHelper("WorkOrderList").RenderWorkOrderList(ViewData);
            return Content(html);
        }

        public ContentResult EventList()
        {
            //客户公司
            var companys = TempData["Companys"];
            if (companys == null)
            {
                companys = siteBranch.GetSiteALLBranch().Where(x => !string.IsNullOrEmpty(x.Alias)).ToList();
            }
            ViewData["Companys"] = companys;
            TempData["Companys"] = companys;

            var isGlobal = TempData["IsGlobalUser"];
            var currentCompany = TempData["CurrentCompany"] as SiteBranchModel;

            if (isGlobal == null || currentCompany == null)
            {
                var CurrentUser = user_x.GetUser_XByName(User.Identity.Name);
                if (CurrentUser == null)
                {
                    throw new Exception("USER_X is null.");
                }
                var sites = user_x.GetSiteBranchByID(CurrentUser.ID);
                var t = sites.FirstOrDefault(s => s.Name == CurrentUser.Company);
                if (t == null)
                {
                    throw new Exception("UserSite is null.");
                }
                isGlobal = t.Alias.Equals("省局");
                currentCompany = t;
            }

            //省局用户
            ViewData["IsGlobalUser"] = isGlobal;
            TempData["IsGlobalUser"] = isGlobal;

            //用户所在公司
            ViewData["CurrentCompany"] = currentCompany;
            TempData["CurrentCompany"] = currentCompany;

            var html = this.GetHtmlHelper("EventList").RenderEventList(ViewData);
            return Content(html);
        }

        public ContentResult ChangeList()
        {
            //客户公司
            var companys = TempData["Companys"];
            if (companys == null)
            {
                companys = siteBranch.GetSiteALLBranch().Where(x => !string.IsNullOrEmpty(x.Alias)).ToList();
            }
            ViewData["Companys"] = companys;
            TempData["Companys"] = companys;

            var isGlobal = TempData["IsGlobalUser"];
            var currentCompany = TempData["CurrentCompany"] as SiteBranchModel;

            if (isGlobal == null || currentCompany == null)
            {
                var CurrentUser = user_x.GetUser_XByName(User.Identity.Name);
                if (CurrentUser == null)
                {
                    throw new Exception("USER_X is null.");
                }
                var sites = user_x.GetSiteBranchByID(CurrentUser.ID);
                var t = sites.FirstOrDefault(s => s.Name == CurrentUser.Company);
                if (t == null)
                {
                    throw new Exception("UserSite is null.");
                }
                isGlobal = t.Alias.Equals("省局");
                currentCompany = t;
            }

            //省局用户
            ViewData["IsGlobalUser"] = isGlobal;
            TempData["IsGlobalUser"] = isGlobal;

            //用户所在公司
            ViewData["CurrentCompany"] = currentCompany;
            TempData["CurrentCompany"] = currentCompany;

            var html = this.GetHtmlHelper("ChangeList").RenderChangeList(ViewData);
            return Content(html);
        }

        public ContentResult TaskList()
        {
            Thread.CurrentThread.CurrentCulture = Culture;
            Thread.CurrentThread.CurrentUICulture = Culture;

            //客户公司
            var companys = TempData["Companys"];
            if (companys == null)
            {
                companys = siteBranch.GetSiteALLBranch().Where(x => !string.IsNullOrEmpty(x.Alias)).ToList();
            }
            ViewData["Companys"] = companys;
            TempData["Companys"] = companys;

            var isGlobal = TempData["IsGlobalUser"];
            var currentCompany = TempData["CurrentCompany"] as SiteBranchModel;

            if (isGlobal == null || currentCompany == null)
            {
                var CurrentUser = user_x.GetUser_XByName(User.Identity.Name);
                if (CurrentUser == null)
                {
                    throw new Exception("USER_X is null.");
                }
                var sites = user_x.GetSiteBranchByID(CurrentUser.ID);
                var t = sites.FirstOrDefault(s => s.Name == CurrentUser.Company);
                if (t == null)
                {
                    throw new Exception("UserSite is null.");
                }
                isGlobal = t.Alias.Equals("省局");
                currentCompany = t;
            }

            //省局用户
            ViewData["IsGlobalUser"] = isGlobal;
            TempData["IsGlobalUser"] = isGlobal;

            //用户所在公司
            ViewData["CurrentCompany"] = currentCompany;
            TempData["CurrentCompany"] = currentCompany;

            ViewData["TaskTypes"] = new EnumDataSource<EnumUtility.TaskType>();
            ViewData["TaskStatus"] = new EnumDataSource<EnumUtility.TaskStatus>();

            var html = this.GetHtmlHelper("TaskList").RenderTaskList(ViewData);
            return Content(html);
        }

        public ContentResult AlertList()
        {
            //客户公司
            var companys = TempData["Companys"];
            if (companys == null)
            {
                companys = siteBranch.GetSiteALLBranch().Where(x => !string.IsNullOrEmpty(x.Alias)).ToList();
            }
            ViewData["Companys"] = companys;
            TempData["Companys"] = companys;

            var isGlobal = TempData["IsGlobalUser"];
            var currentCompany = TempData["CurrentCompany"] as SiteBranchModel;

            if (isGlobal == null || currentCompany == null)
            {
                var CurrentUser = user_x.GetUser_XByName(User.Identity.Name);
                if (CurrentUser == null)
                {
                    throw new Exception("USER_X is null.");
                }
                var sites = user_x.GetSiteBranchByID(CurrentUser.ID);
                var t = sites.FirstOrDefault(s => s.Name == CurrentUser.Company);
                if (t == null)
                {
                    throw new Exception("UserSite is null.");
                }
                isGlobal = t.Alias.Equals("省局");
                currentCompany = t;
            }

            //省局用户
            ViewData["IsGlobalUser"] = isGlobal;
            TempData["IsGlobalUser"] = isGlobal;

            //用户所在公司
            ViewData["CurrentCompany"] = currentCompany;
            TempData["CurrentCompany"] = currentCompany;
            var html = this.GetHtmlHelper("IncidentList").RenderAlertList(ViewData);
            return Content(html);
        }

        public ContentResult HardwareList()
        {
            //客户公司
            var companys = TempData["Companys"];
            if (companys == null)
            {
                companys = siteBranch.GetSiteALLBranch().Where(x => !string.IsNullOrEmpty(x.Alias)).ToList();
            }
            ViewData["Companys"] = companys;
            TempData["Companys"] = companys;

            var isGlobal = TempData["IsGlobalUser"];
            var currentCompany = TempData["CurrentCompany"] as SiteBranchModel;

            if (isGlobal == null || currentCompany == null)
            {
                var CurrentUser = user_x.GetUser_XByName(User.Identity.Name);
                if (CurrentUser == null)
                {
                    throw new Exception("USER_X is null.");
                }
                var sites = user_x.GetSiteBranchByID(CurrentUser.ID);
                var t = sites.FirstOrDefault(s => s.Name == CurrentUser.Company);
                if (t == null)
                {
                    throw new Exception("UserSite is null.");
                }
                isGlobal = t.Alias.Equals("省局");
                currentCompany = t;
            }

            //省局用户
            ViewData["IsGlobalUser"] = isGlobal;
            TempData["IsGlobalUser"] = isGlobal;

            //用户所在公司
            ViewData["CurrentCompany"] = currentCompany;
            TempData["CurrentCompany"] = currentCompany;
            var html = this.GetHtmlHelper("HardwareList").RenderHardwareList(ViewData);
            return Content(html);
        }

        public ContentResult SoftwareList()
        {
            //客户公司
            var companys = TempData["Companys"];
            if (companys == null)
            {
                companys = siteBranch.GetSiteALLBranch().Where(x => !string.IsNullOrEmpty(x.Alias)).ToList();
            }
            ViewData["Companys"] = companys;
            TempData["Companys"] = companys;

            var isGlobal = TempData["IsGlobalUser"];
            var currentCompany = TempData["CurrentCompany"] as SiteBranchModel;

            if (isGlobal == null || currentCompany == null)
            {
                var CurrentUser = user_x.GetUser_XByName(User.Identity.Name);
                if (CurrentUser == null)
                {
                    throw new Exception("USER_X is null.");
                }
                var sites = user_x.GetSiteBranchByID(CurrentUser.ID);
                var t = sites.FirstOrDefault(s => s.Name == CurrentUser.Company);
                if (t == null)
                {
                    throw new Exception("UserSite is null.");
                }
                isGlobal = t.Alias.Equals("省局");
                currentCompany = t;
            }

            //省局用户
            ViewData["IsGlobalUser"] = isGlobal;
            TempData["IsGlobalUser"] = isGlobal;

            //用户所在公司
            ViewData["CurrentCompany"] = currentCompany;
            TempData["CurrentCompany"] = currentCompany;
            var html = this.GetHtmlHelper("SoftwareList").RenderSoftwareList(ViewData);
            return Content(html);
        }

        public ContentResult BusinessSystemList()
        {
            //客户公司
            var companys = TempData["Companys"];
            if (companys == null)
            {
                companys = siteBranch.GetSiteALLBranch().Where(x => !string.IsNullOrEmpty(x.Alias)).ToList();
            }
            ViewData["Companys"] = companys;
            TempData["Companys"] = companys;

            var isGlobal = TempData["IsGlobalUser"];
            var currentCompany = TempData["CurrentCompany"] as SiteBranchModel;

            if (isGlobal == null || currentCompany == null)
            {
                var CurrentUser = user_x.GetUser_XByName(User.Identity.Name);
                if (CurrentUser == null)
                {
                    throw new Exception("USER_X is null.");
                }
                var sites = user_x.GetSiteBranchByID(CurrentUser.ID);
                var t = sites.FirstOrDefault(s => s.Name == CurrentUser.Company);
                if (t == null)
                {
                    throw new Exception("UserSite is null.");
                }
                isGlobal = t.Alias.Equals("省局");
                currentCompany = t;
            }

            //省局用户
            ViewData["IsGlobalUser"] = isGlobal;
            TempData["IsGlobalUser"] = isGlobal;

            //用户所在公司
            ViewData["CurrentCompany"] = currentCompany;
            TempData["CurrentCompany"] = currentCompany;
            var html = this.GetHtmlHelper("BusinessSystemList").RenderBusinessSystemList(ViewData);
            return Content(html);
        }

        /// <summary>
        /// 获取我选择要展现的列
        /// </summary>
        /// <param name="QueryType"></param>
        /// <returns></returns>
        public JsonResult GetMySelectedColumns(string QueryType)
        {
            SetRoles(user_x);
            var columns = reportView.GetDataColumnByUserID(UserID, QueryType);
            var allColumns = reportView.GetAllColumnsByQueryType(QueryType);

            return Json(new { selectedColumns = columns.Where(x => x.IsSelected == 1).ToList(), allColumns = allColumns }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 更新我的选择
        /// </summary>
        /// <param name="QueryType"></param>
        /// <param name="Columns"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult UpdateSelectedColumns(string QueryType, List<ReportViewUserSet> Columns)
        {
            SetRoles(user_x);
            reportView.UpdateSelectedColumns(UserID, QueryType, Columns);

            return Json("", JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 获取事件性质
        /// </summary>
        /// <returns></returns>
        public JsonResult GetEventProperties()
        {
            var list = reportView.GetEventProperties();

            return Json(new { EventProperty = list }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 获取事件分类
        /// </summary>
        /// <param name="EventProperty"></param>
        /// <returns></returns>
        public JsonResult GetEventType1(string EventProperty)
        {
            var list = reportView.GetEventType1AccordingEventProperty(EventProperty);

            return Json(new { EventType1 = list }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 事件搜索
        /// </summary>
        /// <param name="query"></param>
        /// <param name="page"></param>
        /// <param name="rows"></param>
        /// <param name="sort"></param>
        /// <param name="order"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult SearchEventList(EventQueryString query, int page, int rows, string sort, string order)
        {

            if (string.IsNullOrEmpty(sort)) { sort = "Incident_Number"; }
            if (string.IsNullOrEmpty(order)) { order = "Descending"; }
            decimal total = 0;

            var list = reportView.SearchEventList(query, out total, page, rows, sort, order);

            return Json(new { rows = list, total = total }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 事件分组
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult GroupEventList(EventQueryString query)
        {
            SetRoles(user_x);
            var groupName = "";
            var groupCode = "";
            decimal total = 0;
            var groups = reportView.GroupEventList(UserID, query, out groupName, out groupCode);

            if (groups != null && groups.Count > 0)
            {
                total = groups.Sum(x => x.Count);
            }

            return Json(new { rows = groups, total = total, groupName = groupName, groupCode = groupCode }, JsonRequestBehavior.AllowGet);
        }

        public JsonResult GetDepartmentListWithCompany(string company)
        {
            var list = reportView.GetDepartmentListWithCompany(company);

            return Json(new { departments = list }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 合并工单查询
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult SearchWorkOrderList(WorkOrderQueryString query, string sort, string order)
        {
            if (string.IsNullOrEmpty(sort)) { sort = "Department"; }
            if (string.IsNullOrEmpty(order)) { order = "Descending"; }
            List<WorkOrderView> result = reportView.SearchWorkOrderList(query, sort, order);
            return Json(new { rows = result, total = result.Count }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 任务分组统计
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult GroupTaskList(TaskQueryString query)
        {
            SetRoles(user_x);
            var groupName = "";
            var groupCode = "";
            decimal total = 0;
            var groups = reportView.GroupTaskList(UserID, query, out groupName, out groupCode);

            if (groups != null && groups.Count > 0)
            {
                total = groups.Sum(x => x.Count);
            }

            return Json(new { rows = groups, total = total, groupName = groupName, groupCode = groupCode }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 任务搜索
        /// </summary>
        /// <param name="query"></param>
        /// <param name="page"></param>
        /// <param name="rows"></param>
        /// <param name="sort"></param>
        /// <param name="order"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult SearchTaskList(TaskQueryString query, int page, int rows, string sort, string order)
        {
            if (string.IsNullOrEmpty(sort)) { sort = "JobNo"; }
            if (string.IsNullOrEmpty(order)) { order = "Descending"; }
            decimal total = 0;

            var list = reportView.SearchTaskList(query, out total, page, rows, sort, order);

            return Json(new { rows = list, total = total }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 变更分组统计
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult GroupChangeList(ChangeQueryString query)
        {
            SetRoles(user_x);
            var groupName = "";
            var groupCode = "";
            decimal total = 0;
            var groups = reportView.GroupChangeList(UserID, query, out groupName, out groupCode);

            if (groups != null && groups.Count > 0)
            {
                total = groups.Sum(x => x.Count);
            }

            return Json(new { rows = groups, total = total, groupName = groupName, groupCode = groupCode }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 变更搜索
        /// </summary>
        /// <param name="query"></param>
        /// <param name="page"></param>
        /// <param name="rows"></param>
        /// <param name="sort"></param>
        /// <param name="order"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult SearchChangeList(ChangeQueryString query, int page, int rows, string sort, string order)
        {
            if (string.IsNullOrEmpty(sort)) { sort = "Infrastructure_Change_ID"; }
            if (string.IsNullOrEmpty(order)) { order = "Descending"; }
            decimal total = 0;

            var list = reportView.SearchChangeList(query, out total, page, rows, sort, order);

            return Json(new { rows = list, total = total }, JsonRequestBehavior.AllowGet);
        }



        /// <summary>
        /// 硬件分组统计
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult GroupHardwareList(HardwareQueryString query)
        {
            SetRoles(user_x);
            var groupName = "";
            var groupCode = "";
            decimal total = 0;
            var groups = reportView.GroupHardwareList(UserID, query, out groupName, out groupCode);

            if (groups != null && groups.Count > 0)
            {
                total = groups.Sum(x => x.Count);
            }

            return Json(new { rows = groups, total = total, groupName = groupName, groupCode = groupCode }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 硬件搜索
        /// </summary>
        /// <param name="query"></param>
        /// <param name="page"></param>
        /// <param name="rows"></param>
        /// <param name="sort"></param>
        /// <param name="order"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult SearchHardwareList(HardwareQueryString query, int page, int rows, string sort, string order)
        {
            if (string.IsNullOrEmpty(sort)) { sort = "Name"; }
            if (string.IsNullOrEmpty(order)) { order = "Descending"; }
            decimal total = 0;

            var list = reportView.SearchHardwareList(query, out total, page, rows, sort, order);

            return Json(new { rows = list, total = total }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 软件分组统计
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult GroupSoftwareList(SoftwareQueryString query)
        {
            SetRoles(user_x);
            var groupName = "";
            var groupCode = "";
            decimal total = 0;
            var groups = reportView.GroupSoftwareList(UserID, query, out groupName, out groupCode);

            if (groups != null && groups.Count > 0)
            {
                total = groups.Sum(x => x.Count);
            }

            return Json(new { rows = groups, total = total, groupName = groupName, groupCode = groupCode }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 软件搜索
        /// </summary>
        /// <param name="query"></param>
        /// <param name="page"></param>
        /// <param name="rows"></param>
        /// <param name="sort"></param>
        /// <param name="order"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult SearchSoftwareList(SoftwareQueryString query, int page, int rows, string sort, string order)
        {
            if (string.IsNullOrEmpty(sort)) { sort = "Name"; }
            if (string.IsNullOrEmpty(order)) { order = "Descending"; }
            decimal total = 0;

            var list = reportView.SearchSoftwareList(query, out total, page, rows, sort, order);

            return Json(new { rows = list, total = total }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 应用系统分组统计
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult GroupBusinessSystemList(BusinessSystemQueryString query)
        {
            SetRoles(user_x);
            var groupName = "";
            var groupCode = "";
            decimal total = 0;
            var groups = reportView.GroupBusinessSystemList(UserID, query, out groupName, out groupCode);

            if (groups != null && groups.Count > 0)
            {
                total = groups.Sum(x => x.Count);
            }

            return Json(new { rows = groups, total = total, groupName = groupName, groupCode = groupCode }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 应用系统搜索
        /// </summary>
        /// <param name="query"></param>
        /// <param name="page"></param>
        /// <param name="rows"></param>
        /// <param name="sort"></param>
        /// <param name="order"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult SearchBusinessSystemList(BusinessSystemQueryString query, int page, int rows, string sort, string order)
        {
            if (string.IsNullOrEmpty(sort)) { sort = "Name"; }
            if (string.IsNullOrEmpty(order)) { order = "Descending"; }
            decimal total = 0;

            var list = reportView.SearchBusinessSystemList(query, out total, page, rows, sort, order);

            return Json(new { rows = list, total = total }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 告警分组统计
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult GroupAlertList(AlertQueryString query)
        {
            SetRoles(user_x);
            var groupName = "";
            var groupCode = "";
            decimal total = 0;
            var groups = reportView.GroupAlertList(UserID, query, out groupName, out groupCode);

            if (groups != null && groups.Count > 0)
            {
                total = groups.Sum(x => x.Count);
            }

            return Json(new { rows = groups, total = total, groupName = groupName, groupCode = groupCode }, JsonRequestBehavior.AllowGet);
        }

        /// <summary>
        /// 告警搜索
        /// </summary>
        /// <param name="query"></param>
        /// <param name="page"></param>
        /// <param name="rows"></param>
        /// <param name="sort"></param>
        /// <param name="order"></param>
        /// <returns></returns>
        [HttpPost]
        public JsonResult SearchAlertList(AlertQueryString query, int page, int rows, string sort, string order)
        {
            if (string.IsNullOrEmpty(sort)) { sort = "CreateTime"; }
            if (string.IsNullOrEmpty(order)) { order = "Descending"; }
            decimal total = 0;

            var list = reportView.SearchAlertList(query, out total, page, rows, sort, order);

            return Json(new { rows = list, total = total }, JsonRequestBehavior.AllowGet);
        }


        public ActionResult ExportReportViewToExcel()
        {
            return View();
        }

        /// <summary>
        /// 将报表内容导出到excel里面
        /// </summary>
        /// <param name="param"></param>
        [HttpPost]
        public void ExportReportViewToExcel(string queryType, string reportViewData, string queryString, string order, string sort)
        {
            if (!string.IsNullOrEmpty(reportViewData))
            {
                var reportData = JsonConvert.DeserializeObject<ReportViewData>(reportViewData);
                DataTable data = null;
                switch (queryType)
                {
                    case "事件综合":
                        data = reportView.SearchEventList_Report(JsonConvert.DeserializeObject<EventQueryString>(queryString), 1, 10000, sort, order);
                        break;
                    case "变更综合":
                        data = reportView.SearchChangeList_Report(JsonConvert.DeserializeObject<ChangeQueryString>(queryString), 1, 10000, sort, order);
                        break;
                    case "任务综合":
                        data = reportView.SearchTaskList_Report(JsonConvert.DeserializeObject<TaskQueryString>(queryString), 1, 10000, sort, order);
                        break;
                    case "告警综合":
                        data = reportView.SearchAlertList_Export(JsonConvert.DeserializeObject<AlertQueryString>(queryString), 1, 10000, sort, order);
                        break;
                    case "硬件综合":
                        data = reportView.SearchHardwareList_Report(JsonConvert.DeserializeObject<HardwareQueryString>(queryString), 1, 10000, sort, order);
                        break;
                    case "软件综合":
                        data = reportView.SearchSoftwareList_Report(JsonConvert.DeserializeObject<SoftwareQueryString>(queryString), 1, 10000, sort, order);
                        break;
                    case "业务系统综合":
                        data = reportView.SearchBusinessSystemList_Report(JsonConvert.DeserializeObject<BusinessSystemQueryString>(queryString), 1, 10000, sort, order);
                        break;
                }

                RenderReportExcel(queryType, reportData, data);
            }
        }

        /// <summary>
        /// 生成excel文件
        /// </summary>
        /// <param name="queryType"></param>
        /// <param name="reportViewData"></param>
        /// <param name="detailData"></param>

        public void RenderReportExcel(string queryType, ReportViewData reportViewData, DataTable detailData)
        {
            if (reportViewData != null)
            {
                HSSFWorkbook workbook = new HSSFWorkbook();

                //分组数据
                HSSFSheet sheet_group = (HSSFSheet)workbook.CreateSheet("汇总维度(" + reportViewData.GroupName + ")");
                HSSFRow dataRow = (HSSFRow)sheet_group.CreateRow(0);
                for (var i = 0; i < reportViewData.GroupTitleList.Count; i++)
                {
                    dataRow.CreateCell(i).SetCellValue(reportViewData.GroupTitleList[i]);
                }

                for (int i = 0; i < reportViewData.GroupDataList.Count; i++)
                {
                    dataRow = (HSSFRow)sheet_group.CreateRow(i + 1);
                    var rowData = reportViewData.GroupDataList[i].Split(',');
                    for (int j = 0; j < rowData.Count(); j++)
                    {
                        dataRow.CreateCell(j).SetCellValue(rowData[j]);
                    }
                }

                if (detailData != null)
                {
                    HSSFSheet sheet_data = (HSSFSheet)workbook.CreateSheet("详情数据");
                    dataRow = (HSSFRow)sheet_data.CreateRow(0);

                    for (var i = 0; i < reportViewData.DetailTitleList.Count; i++)
                    {
                        dataRow.CreateCell(i).SetCellValue(reportViewData.DetailTitleList[i]);
                    }

                    detailData = new DataView(detailData).ToTable(false, reportViewData.DetailFieldList.ToArray());
                    for (int i = 0; i < detailData.Rows.Count; i++)
                    {
                        dataRow = (HSSFRow)sheet_data.CreateRow(i + 1);
                        for (int j = 0; j < detailData.Columns.Count; j++)
                        {
                            dataRow.CreateCell(j).SetCellValue(detailData.Rows[i][j].ToString());
                        }
                    }
                }

                var fileFullPath = Path.Combine(CreateUploadFilePath(), queryType + "报表.xls");
                using (FileStream stream = System.IO.File.OpenWrite(fileFullPath))
                {
                    //写入临时文件
                    workbook.Write(stream);
                }

                ExportFileToResponse(fileFullPath);

                Response.Close();

            }


        }

    }
}
